package org.lex.keytool.ui;

import java.awt.BorderLayout;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.plaf.SplitPaneUI;
import javax.swing.plaf.basic.BasicSplitPaneUI;

import org.lex.keytool.core.task.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MainContentPane extends JPanel {
	private JSplitPane splitPane = null;
	private MenuTree menuTree = null;
	private WorkspacePane workspacePane = null;
	private Director director = null;

	public MainContentPane() {
		initComponents();
	}

	private void initComponents() {
		this.setLayout(new BorderLayout(0, 0));
		this.add(this.getSplitPane(), java.awt.BorderLayout.CENTER);
	}

	private JSplitPane getSplitPane() {
		if (null == splitPane) {
			splitPane = new JSplitPane();
			SplitPaneUI ui = splitPane.getUI();
			if (ui instanceof BasicSplitPaneUI) {
				// ((BasicSplitPaneUI) ui).getDivider().setBorder(null);
			}
			splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
			splitPane.setDividerSize(6);
			splitPane.setResizeWeight(0.3);
			splitPane.add(new JScrollPane(getMenuTree()), JSplitPane.LEFT);
			splitPane.add(getWorkspacePane(), JSplitPane.RIGHT);
		}
		return splitPane;
	}

	private MenuTree getMenuTree() {
		if (null == menuTree) {
			menuTree = new MenuTree();
			menuTree.addMenuChangeListener(this.getDirector());
		}
		return menuTree;
	}

	private WorkspacePane getWorkspacePane() {
		if (null == workspacePane) {
			workspacePane = new WorkspacePane();
		}
		return workspacePane;
	}

	private Director getDirector() {
		if (null == director) {
			this.director = new Director();
		}
		return director;
	}

	private class Director implements MenuTree.MenuChangeListener {
		private Logger log = LoggerFactory.getLogger(Director.class);

		@Override
		public void selectedMenuChanged(Class<? extends Task> taskClass) {
			log.info("selected task class=" + taskClass);
			getWorkspacePane().setTaskClass(taskClass);
		}
	}
}
